<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Titemplate extends Common
{

	//列表
    public function index()
    {

        $where = [];
        $uid = session('employee_id');
        $where['ispublic'] = ['exp','=1 or (ispublic = 0 and cuser = '.$uid.')'];

        $title = $this->request->get('title');
        $this->assign('title',$title);
        if($title){
            $where['title'] = ['like',"%$title%"];
        }

        $count = Db::table('cg_ti_template t')->where($where)->count();
        $data = Db::table('cg_ti_template t')->order("t.id desc")->where($where)->paginate(10,$count);

//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('employee')->where(array('id'=>$v['cuser']))->value('em_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    public function add(){

        if($_POST){

            $validate = new Valid([
                'title|模板标题'  => 'require|max:200',
                'ispublic|是否公用模板'  => 'require',
                'brief|模板内容'  => 'require',
            ]);
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['ispublic'] = $this->request->post('ispublic');
            $data['brief'] = $this->request->post('brief');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = session('employee_id');
            $data['ctime'] = time();
            $data['ptime'] = time();

            $bool = Db::table('cg_ti_template')->insert($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"添加成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"添加失败"
                ]);die;
            }

        }


        return $this->fetch('form');
    }

    /**
     * 修改基本信息
     * @return mixed
     * @throws \think\Exception
     */
    public function update(){
        $id = $this->request->param('id');

        if($_POST){

            $validate = new Valid([
                'title|模板标题'  => 'require|max:200',
                'ispublic|是否公用模板'  => 'require',
                'brief|模板内容'  => 'require',
            ]);
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['ispublic'] = $this->request->post('ispublic');
            $data['brief'] = $this->request->post('brief');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = session('employee_id');
            $data['ctime'] = time();
            $data['ptime'] = time();

            $bool = Db::table('cg_ti_template')->where('id',$id)->update($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"修改成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"修改失败"
                ]);die;
            }

        }

        $data = db('cg_ti_template')->where('id',$id)->find();

        $this->assign('data',$data);

        return $this->fetch('form');
    }

    /**
     * 删除
     * @throws \think\Exception
     */
    public function delete(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_ti_template')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

    /**
     * 查找单条数据替换当前内容
     */
    public function find(){
        $id = $this->request->param('id');
        $data = db('cg_ti_template')->where('id',$id)->field('title,brief')->find();

        $cnt = db('cg_ti_template_cy')->where(['tid'=>$id,'cuser'=>session('employee_id')])->count();
        if($cnt){
            db('cg_ti_template_cy')->where(['tid'=>$id,'cuser'=>session('employee_id')])->setInc('num');
        }else{
            db('cg_ti_template_cy')->insert([
                'tid'=>$id,
                'cuser'=>session('employee_id')
            ]);
        }
        echo  json_encode([
            "success"=>true,
            "title"=>$data['title'],
            "brief"=>$data['brief'],
        ]);die;
    }

}
